package com.ssm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ssm.mapper.AddressMapper;
import com.ssm.pojo.address.Address;
import com.ssm.pojo.address.AddressDeleteOrDefualtResponseParameter;
import com.ssm.pojo.address.AddressList;
import com.ssm.pojo.address.AddressRequestParameter;
import com.ssm.pojo.address.AddressResponseParameter;
import com.ssm.service.AddressService;
// 自动注册到Spring容器
@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    AddressMapper addressMapper;

	@Override
	public AddressList list(int userId) {
		// TODO Auto-generated method stub
		List<Address> address = addressMapper.list(userId);
		AddressList addressList = new AddressList();
		addressList.setContectList(address);
		return addressList;
	}

	@Override
	public AddressDeleteOrDefualtResponseParameter delete(AddressRequestParameter data) {
		// TODO Auto-generated method stub
		Integer result = addressMapper.delete(data);
		// 响应参数
		AddressDeleteOrDefualtResponseParameter parameter = new AddressDeleteOrDefualtResponseParameter();
		if (result.equals(1)) {
			parameter.setErrorCode(0);
			parameter.setErrorMsg("删除地址成功");
			parameter.setData(data);
			return parameter;
		}
		parameter.setErrorCode(1);
		parameter.setErrorMsg("删除地址失败");
		parameter.setData(data);
		return parameter;
	}

	@Override
	public AddressResponseParameter add(Address address) {
		// TODO Auto-generated method stub
		Integer contactFlag = address.getContactFlag();
		if(contactFlag.equals(1)) addressMapper.clearDefaultAddr(address.getUserId());
		Integer result = addressMapper.add(address);
		AddressResponseParameter parameter = new AddressResponseParameter();
		if (result.equals(1)) {
			parameter.setErrorCode(0);
			parameter.setErrorMsg("添加成功");
			parameter.setData(address);
			return parameter;
		}
		parameter.setErrorCode(1);
		parameter.setErrorMsg("添加失败");
		parameter.setData(address);
		return parameter;
	}

	@Override
	public Address get(int id) {
		// TODO Auto-generated method stub
		Address address = addressMapper.get(id);
		return address;
	}

	@Override
	public AddressResponseParameter update(Address address) {
		// TODO Auto-generated method stub
		Integer contactFlag = address.getContactFlag();
		if(contactFlag.equals(1)) addressMapper.clearDefaultAddr(address.getUserId());
		Integer result = addressMapper.update(address);
		AddressResponseParameter parameter = new AddressResponseParameter();
		if (result.equals(1)) {
			parameter.setErrorCode(0);
			parameter.setErrorMsg("修改成功");
			parameter.setData(address);
			return parameter;
		}
		parameter.setErrorCode(1);
		parameter.setErrorMsg("修改失败");
		parameter.setData(address);
		return parameter;
	}

	@Override
	public AddressDeleteOrDefualtResponseParameter defaultAddr(AddressRequestParameter data) {
		// TODO Auto-generated method stub
		addressMapper.clearDefaultAddr(data.getUserId());
		Integer result = addressMapper.defaultAddr(data);
		// 响应参数
		AddressDeleteOrDefualtResponseParameter parameter = new AddressDeleteOrDefualtResponseParameter();
		if (result.equals(1)) {
			parameter.setErrorCode(0);
			parameter.setErrorMsg("修改默认地址成功");
			parameter.setData(data);
			return parameter;
		}
		parameter.setErrorCode(1);
		parameter.setErrorMsg("修改默认地址失败");
		parameter.setData(data);
		return parameter;
	}

}
